const baseUrl = ''; 
const httpRequest = (opts, data,requestHeader) => {
    let httpDefaultOpts = {
        url: baseUrl+opts.url,
        data: data,
        method: opts.method,
        header:{
			"Content-Type":requestHeader,
		},
       /* dataType: 'json', */
    }
    let promise = new Promise(function(resolve, reject) {
        uni.request(httpDefaultOpts).then(
            (res) => {
                resolve(res[1])
            }
        ).catch(
            (response) => {
                reject(response)
            }
        )
    })
    return promise
};
//带Token请求
const httpTokenRequest = (opts, data) => {
	let token = "";
	uni.getStorage({
		key: 'token',
		success: function(ress) {
			token = ress.data
		}
	});
    //此token是登录成功后后台返回保存在storage中的
    let httpDefaultOpts = {
        url: baseUrl+opts.url,
        data: data,
        method: opts.method,
        /* header: opts.method == 'GET' ? {
		'Token': token,
        'X-Requested-With': 'XMLHttpRequest',
        "Accept": "application/json",
        "Content-Type": "multipart/form-data;application/json; charset=UTF-8"
    } : {
		'Token': token,
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    },
        dataType: 'json', */
    }
    let promise = new Promise(function(resolve, reject) {
        uni.request(httpDefaultOpts).then(
            (res) => {
                resolve(res[1])
            }
        ).catch(
            (response) => {
                reject(response)
            }
        )
    })
    return promise
};
 
export default {
	    baseUrl,
		httpRequest,
		httpTokenRequest					
}


           /* 使用方法如下:
			let opts={
                        url: '/api/device/add',
                        method: 'post'
                    };
                    let param={
                        deviceId:this.deviceCode,
                        deviceName:this.deviceName
                    };
                    http.httpRequest(opts, param,requestHeader).then(res => {
                        console.log(res.data);
                       //打印请求返回的数据
                        
                    },error => {console.log(error);})  */
